﻿using Autofac;
using System.Configuration;
using Microsoft.EntityFrameworkCore;
using Super.Hmi.EFCore;

namespace Super.Hmi.Modules
{
    public class DataBaseModule : Module
    {
        /// <summary>
        /// 加载数据库模块
        /// </summary>
        /// <param name="builder"></param>
        protected override void Load(ContainerBuilder builder)
        {
            // 从配置文件中获取连接字符串
            string connectionDataBaseString = ConfigurationManager.AppSettings["SuperDatabase"]!;
            // 配置主数据库选项
            var dataBaseOptions = new DbContextOptionsBuilder<SuperDataBaseDbContext>().UseSqlite(connectionDataBaseString).Options;
            // 注册主数据库上下文
            builder.RegisterType<SuperDataBaseDbContext>()
                .WithParameter(new TypedParameter(typeof(DbContextOptions<SuperDataBaseDbContext>), dataBaseOptions))
                .InstancePerLifetimeScope();
        }
    }
}
